<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加设备点位' : '编辑设备点位'" :visible.sync="addEditIsDialog_c" :close-on-click-modal="false" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="点位名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入点位名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="点位位号：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入点位位号" clearable maxlength="200" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位编码：" prop="dataPointCode">
              <el-select filterable v-model="ruleForm.dataPointCode" placeholder="请选择点位编码" clearable :disabled="seeType ">
                <el-option v-for="item in dataPointsList" :key="item.id" :label="item.pointCode" :value="item.pointCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="设备：" prop="examinationRoomId">
              <el-select v-model="ruleForm.examinationRoomId" placeholder="请选择设备名称" clearable :disabled="seeType ">
                <el-option v-for="item in schoolArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="单位：" prop="unit">
              <el-input v-model="ruleForm.unit" placeholder="请输入单位" clearable maxlength="200" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序号：" prop="orderNum">
              <el-input-number v-model="ruleForm.orderNum" placeholder="请输入排序号" :precision="0" :step="1" :max="9999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="点位类型：" prop="type">
              <el-select v-model="ruleForm.type" placeholder="请选择点位类型" clearable :disabled="seeType ">
                <el-option label="开关量点位" :value="0"></el-option>
                <el-option label="数值" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="ruleForm.type==0">
          <el-col :span="12">
            <el-form-item label="开启量：" prop="startSize">
              <el-input v-model="ruleForm.startSize" placeholder="请输入开启量" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关闭量：" prop="stopSize">
              <el-input v-model="ruleForm.stopSize" placeholder="请输入关闭量" :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getDevicePoint, saveDevicePoint, getDeviceInfos } from '@/api/device'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    examinationRoomId: {
      default: ''
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    },
    dataPointsList: {}
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        deviceId: '',
        name: '',
        code: '',
        unit: '',
        examinationRoomId: '',
        orderNum: '',
        dataPointCode: '',
        type: '',
        startSize: '',
        stopSize: ''
      },
      schoolArray: [],
      rules: {
        // 表单规则
        name: [{ required: true, message: '请输入点位名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入点位位号', trigger: 'blur' }],
        dataPointCode: [{ required: true, message: '请输入数据点位编码', trigger: 'blur' }],
        unit: [{ required: true, message: '请输入单位', trigger: 'blur' }],
        deviceId: [{ required: true, message: '请选择设备', trigger: 'change' }],
        type: [{ required: true, message: '请选择点位类型', trigger: 'change' }],
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      pointIndexArray: [], // 点位指标集合
      dataPointArray: [], // 数据点位集合
      alarmStatusArray: [] // 报警状态集合
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }
        this.getDeviceInfos()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getDevicePoint({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.isAlarmUpperBo = res.data.isAlarmUpper != undefined && res.data.isAlarmUpper == 1
          res.data.isAlarmLowerBo = res.data.isAlarmLower != undefined && res.data.isAlarmLower == 1
          this.ruleForm = res.data
        }
      })
    },
    getDeviceInfos() {
      getDeviceInfos({ id: this.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.deviceId = this.$route.query.deviceId
          this.ruleForm.isAlarmUpper = this.ruleForm.isAlarmUpperBo ? 1 : 0
          this.ruleForm.isAlarmLower = this.ruleForm.isAlarmLowerBo ? 1 : 0
          saveDevicePoint(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm.isAlarmLowerBo = false
      this.ruleForm.isAlarmUpperBo = false
      this.$parent.closeAddEdit()
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
